//
//  GetNumView.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/30.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "GetNumView.h"
#import "EditNumVC.h"
//收货地址
#define ADDRESS_URL @"/api/ApiUser/getReceivingAddr"
//编辑收货地址
#define EDIT_ADD_URL @"/api/ApiUser/editReceivingAddr"
#define BEIZHU_STR @"备注信息"
@interface GetNumView ()<UITextViewDelegate>
{
    __weak IBOutlet UIButton *ensurebtn;
    __weak IBOutlet UIButton *editbtn;
    __weak IBOutlet UILabel *l1;
    __weak IBOutlet UILabel *numLab;
    __weak IBOutlet UITextView *beizhuText;
    //记着号码
    NSString *_numQQ;
    NSString *_numPhone;
    NSString *_numZFB;
    NSString *currentNum;
    
    NSString *getTime;
    
    // 收货人
    NSString *add_shouhuoren;
    // 联系手机
    NSString *add_mobile;
    // 省
    NSString *add_sheng;
    // 市
    NSString *add_shi;
    // 县
    NSString *add_xian;
    //详细地址
    NSString *add_xiangxi;
    // 街道地址
    NSString *add_jiedao;
    // 收货地址
    NSString *add_addr;
}
@end
@implementation GetNumView

- (void)awakeFromNib
{
    //分割线
    [self addSubview:[WYPublic getFenGexian:0 :CGRectGetMaxY(l1.frame) :self.frame.size.width*AUTOX :0.5]];
    [self addSubview:[WYPublic getFenGexian:0 :CGRectGetMaxY(beizhuText.frame)+10 :self.frame.size.width*AUTOX :0.5]];
    [self addSubview:[WYPublic getFenGexian:CGRectGetMaxX(ensurebtn.frame) :CGRectGetMaxY(beizhuText.frame)+10.5 :0.5:40]];
    self.layer.cornerRadius = 2;
    self.layer.masksToBounds = YES;
    beizhuText.layer.masksToBounds = YES;
    beizhuText.layer.cornerRadius = 2;
    beizhuText.layer.borderColor = [UIColor lightGrayColor].CGColor;
    beizhuText.layer.borderWidth = 0.5;
    beizhuText.delegate = self;
    [self requestForAddress];

}
//请求收货地址
- (void)requestForAddress
{
    [WYPublic addHudToWindow];
    NSDictionary *parmars = @{@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,ADDRESS_URL] Parameters:parmars successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        NSDictionary *info = dic[@"data"];
        if (ret.intValue == 0) {
            add_addr = info[@"addr"];
            add_jiedao = info[@"jiedao"];
            add_sheng = info[@"sheng"];
            add_shi = info[@"shi"];
            add_xiangxi = info[@"xiangxi"];
            add_xian = info[@"xian"];
            add_mobile = info[@"mobile"];
            add_shouhuoren = info[@"shouhuoren"];
            
            _numQQ = info[@"recharge_qq"];
            _numPhone = info[@"recharge_mobile"];
            _numZFB = info[@"recharge_alipay"];
            [self updateView];
            }else{
            if(ret.intValue>2000 && ret.intValue < 3000){
                //用户过期，强制重新登录 先删除用户信息
                [UserInfo deleteUserInfo];
                //登录
                [WYPublic ifUserHaveLogin];
                [WYPublic showMessageWithTitle:@"提示：" Msg:@"登录信息已过期"];
            }
            
        }
        
        [WYPublic removeHudToWindow];
    } failedBlock:^{
        
        [WYPublic removeHudToWindow];
    } needLog:NO];
}

//刷新
- (void)updateView
{
    if ([self.order_type class] != [NSNull class]) {
        switch ([self.order_type intValue]) {
            case 0:
            {
                
            }
                break;
            case 1:
            {
                if ([_numQQ class] != [NSNull class] && _numQQ != nil) {
                    numLab.attributedText = [WYPublic getRedMiddleStrWithLeft:@"QQ号码:" Middle:[NSString stringWithFormat:@"%@",_numQQ] RightStr:@"" Font:14];
                    if (_numQQ.length == 0) {
                        numLab.text = @"  请编辑充值号码";
                    }
                }else{
                    numLab.text = @"  请编辑充值号码";
                }
                currentNum = _numQQ;
            }
                break;
            case 2:
            {
                if ([_numPhone class] != [NSNull class] && _numPhone != nil) {
                    numLab.attributedText = [WYPublic getRedMiddleStrWithLeft:@"手机号码:" Middle:[NSString stringWithFormat:@"%@",_numPhone] RightStr:@"" Font:14];
                    if (_numPhone.length == 0) {
                        numLab.text = @"  请编辑充值号码";
                    }
                }else{
                    
                    numLab.text = @"  请编辑充值号码";
                }
                currentNum = _numPhone;
            }
                break;
            case 3:
            {
                if ([_numZFB class] != [NSNull class] && _numZFB != nil) {
                    numLab.attributedText = [WYPublic getRedMiddleStrWithLeft:@"支付宝账号:" Middle:[NSString stringWithFormat:@"%@",_numZFB] RightStr:@"" Font:14];
                    if (_numZFB.length == 0) {
                        numLab.text = @"  请编辑充值号码";
                    }
                }else{
                    numLab.text = @"  请编辑充值号码";
                }
                currentNum = _numZFB;
            }
                break;
            default:
                break;
        }
    }
}

//确定按钮的监听
- (IBAction)ensureBtnOnClick:(id)sender {
    if ([add_sheng class] == [NSNull class] ||add_sheng.length == 0 || add_sheng == nil) {
        add_sheng = @"";
    }
    if ([add_shi class] == [NSNull class] ||add_shi.length == 0 || add_shi == nil) {
        add_shi = @"";
    }
    if ([add_xian class] == [NSNull class] ||add_xian.length == 0 || add_xian == nil) {
        add_xian = @"";
    }
    if ([add_xiangxi class] == [NSNull class] ||add_xiangxi.length == 0 || add_xiangxi == nil) {
        add_xiangxi = @"";
    }
    if ([_numPhone class] == [NSNull class] ||_numPhone.length == 0 || _numPhone == nil) {
        _numPhone = @"";
    }
    if ([_numQQ class] == [NSNull class] ||_numQQ.length == 0 || _numQQ == nil) {
        _numQQ = @"";
    }
    if ([_numZFB class] == [NSNull class] ||_numZFB.length == 0 || _numZFB == nil) {
        _numZFB = @"";
    }
    if ([numLab.text isEqualToString:@"  请编辑充值号码"]) {
        [self.father removeZheZhao];
        [self removeFromSuperview];
        return;
    }
    
    NSString *beizhu = beizhuText.text;
    if ([beizhuText.text isEqualToString:BEIZHU_STR]) {
        beizhu = @"";
    }

    [WYPublic addZheZhaoToWindowWithTitle:nil];

    //确认收货地址
    NSDictionary *params = nil;
    if ([WYPublic getUsefullStr:add_shouhuoren].length == 0 && [WYPublic getUsefullStr:add_mobile].length == 0 && [WYPublic getUsefullStr:add_sheng].length == 0 &&[WYPublic getUsefullStr:add_shi].length == 0&&[WYPublic getUsefullStr:add_xian].length == 0&&[WYPublic getUsefullStr:add_xiangxi].length == 0) {
        params = @{@"is_confirm":@"1",@"record_id":self.order_id,@"recharge_mobile":_numPhone,@"recharge_qq":_numQQ,@"recharge_alipay":_numZFB,@"remark":beizhu,@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
    }else{
        params = @{@"is_confirm":@"1",@"record_id":self.order_id,@"shouhuoren":add_shouhuoren,@"mobile":add_mobile,@"sheng":add_sheng,@"shi":add_shi,@"xian":add_xian,@"xiangxi":add_xiangxi,@"recharge_mobile":_numPhone,@"recharge_qq":_numQQ,@"recharge_alipay":_numZFB,@"remark":beizhu,@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
    }
    
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,EDIT_ADD_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        if (ret.intValue == 0) {
            //返回刷新界面
            [self.father fillNumWithQQ:_numQQ Phone:_numPhone ZFB:_numZFB Andtime:dic[@"data"][@"status_time"]];
            [self.father removeZheZhao];
            [self removeFromSuperview];
        }else{
            if(ret.intValue>2000 && ret.intValue < 3000){
                //用户过期，强制重新登录 先删除用户信息
                [UserInfo deleteUserInfo];
                //登录
                [WYPublic ifUserHaveLogin];
                [WYPublic showMessageWithTitle:@"提示：" Msg:@"登录信息已过期"];
            }else{
                [WYPublic showMessageWithTitle:@"错误提示:" Msg:dic[@"msg"]];
            }
            
        }
        
        [WYPublic removeZheZhaoToWindow];
        [self removeFromSuperview];
    } failedBlock:^{
        [WYPublic removeZheZhaoToWindow];
        [self removeFromSuperview];
    } needLog:NO];

}

//去编辑按钮的监听
- (IBAction)editbtnOnClick:(id)sender {
    EditNumVC *editVC = [[EditNumVC alloc]init];
    editVC.isZhongJiangFlag = YES;
    editVC.lastFooter = self.father;
    editVC.order_id = self.father.model.reward_order_id;
    [editVC morenInfoWithPhone:_numPhone QQ:_numQQ ZFB:_numZFB];
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:editVC animated:YES];
    
    //去掉当前界面
    [self.father removeZheZhao];
    [self removeFromSuperview];
}
//备注信息的代理
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:BEIZHU_STR]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@""]){
        textView.text = BEIZHU_STR;
    }
}

@end
